package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.po.Vip;
import com.mall.service.VipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mahaoqi
 * @ClassName VipController
 * @Description TODO
 * @Date 2021/1/5 20:08
 * @Version 1.0
 **/
@RestController
@RequestMapping("/shangcheng")
@Api(tags = "vip相关操作")
public class VipController {

    @Autowired
    private VipService vipService;


    /*
     * @Author mahaoqi
     * @Description 根据id删除
     * @Date 2021/1/6 19:01
     * @Param [vipid]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "删除vip")
    @ApiImplicitParam(name = "vipid",value = "要删除目标vip的id",required = true)
    @RequestMapping(value = "/vipdel",method = RequestMethod.DELETE)
    public ActionResult vipDel(int vipid){
        if(vipService.removeById(vipid)){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(500,"删除失败",null);
    }


    /*
     * @Author mahaoqi
     * @Description 添加vip
     * @Date 2021/1/6 19:06
     * @Param [vip]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "添加vip信息")
    @ApiImplicitParam(name = "vip",value = "没有vipid的对象（json）",required = true)
    @RequestMapping(value = "/vipadd",method = RequestMethod.POST)
    public ActionResult vipadd(@RequestBody Vip vip){
        if(vipService.addVip(vip)){
            return new ActionResult(200,"添加成功",null);
        }
        return new ActionResult(500,"添加失败",null);
    }


    /*
     * @Author mahaoqi
     * @Description 修改
     * @Date 2021/1/7 15:17
     * @Param [vip]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "更新vip信息")
    @ApiImplicitParam(name = "vip",value = "更改后的内容（不能更改vipid）",required = true)
    @RequestMapping(value = "/vipupdate",method = RequestMethod.PUT)
    public ActionResult vipupdate(@RequestBody Vip vip){
        Boolean flag = vipService.modifyVip(vip);
        if(flag!=null){
            if(flag){
                return new ActionResult(200,"修改成功",null);
            }else{
                return new ActionResult(500,"修改失败",null);
            }
        }else{
            return new ActionResult(500,"资源未找到",null);
        }

    }

    /*
     * @Author mahaoqi
     * @Description 单查
     * @Date 2021/1/7 15:17
     * @Param [vipid]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "查询单个vip")
    @ApiImplicitParam(name = "vipid",value = "vipid",required = true,dataType = "Integer")
    @RequestMapping(value = "/vipquery",method = RequestMethod.GET)
    public ActionResult vipquery(Integer vipid){
        Vip vip = vipService.findById(vipid);
        if(vip!=null){
            return new ActionResult(200,"查询成功",vip);
        }
        return new ActionResult(500,"资源未找到",null);
    }

    /*
     * @Author mahaoqi
     * @Description 分页
     * @Date 2021/1/7 15:17
     * @Param [page, limit]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "分页获取vip信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "/viplist",method = RequestMethod.GET)
    public ActionResult viplist(Integer page,Integer pagesize){
        Map result = new HashMap();
        Integer count = vipService.findCount();
        List<Vip> vips = vipService.findList((page-1)*pagesize,pagesize);
        if(vips!=null){
            result.put("count",count);
            result.put("data",vips);
            return new ActionResult(200,"查询成功",result);
        }
        return new ActionResult(500,"资源未找到",null);
    }

    @ApiOperation(value = "批量删除vip")
    @ApiImplicitParam(name = "vipids",value = "要删除的vipids数组",required = true)
    @RequestMapping(value = "/vipdelbatch",method = RequestMethod.DELETE)
    public ActionResult vipdelList(@RequestParam("vipids") Integer[] vipids){
        if(vipService.removeBatch(vipids)){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(500,"删除失败",null);
    }
}
